// pages/index/question/question.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		all:[],
    mine:[],
    loadModel:true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getall()
	},
	gotoadd(){
		wx.navigateTo({
			url: '/pages/index/question/add/add',
		})
	},
	getall(){
		app.request({
      url: app.globalData.apiUrl + '/question/all' ,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          all:res.data.data,
        })
      } else {
        Notify('服务错误，稍后重试');
      }
		})
		var user=wx.getStorageSync('user')
		app.request({
      url: app.globalData.apiUrl + '/question/user/'+user.id ,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          mine:res.data.data,
          loadModel:false
        })
      } else {
        this.setData({
          loadModel: false
        })
        Notify('服务错误，稍后重试');
      }
    })
	},
	gotoans(e){
		var question = JSON.stringify(e.target.dataset.item)
		wx.navigateTo({
			url: '/pages/index/question/detail/detail?question='+question,
		})
	},
	delete(e){
		var id=e.target.dataset.id
		app.request({
      url: app.globalData.apiUrl + '/question/'+id ,
      method: 'DELETE',
    }).then(res => {
      if (res.statusCode == 200) {
				Notify({
          type: 'primary',
          message: '删除成功'
        })
        this.getall()
      } else {
        loadModel: false
        Notify('服务错误，稍后重试');
      }
    })
	},
	gotoUpdate(e){
		var question=JSON.stringify(e.target.dataset.question)
		wx.navigateTo({
			url: '/pages/index/question/update/update?question='+question,
		})
	}
})